package com.yehun.utils;

import sun.misc.BASE64Decoder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Genius on 2017/3/31 0031.
 */
public class ParseUtils {

    public static Integer parseInt(String string) {
        return Integer.parseInt(string);
    }

    public static Integer parseInt(String string, int defaultValue) {
        try {
            return Integer.parseInt(string);
        } catch (Exception e) {
            return defaultValue;
        }
    }


    public static Long parseTimeStamp(String string) {
        try {
            return Date.parse(string);
        } catch (Exception e) {
            return 0L;
        }
    }

    public static Long parseTimeStamp(Date date) {
        try {
            return date.getTime() / 1000;
        } catch (Exception e) {
            return 0L;
        }
    }

    public static Date parseDate(String string) throws ParseException {
        SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        return sdf.parse(string);
    }

    public static Date parseDate(long timeStamp) {
        return new Date(timeStamp * 1000);
    }

    public static Long parseLong(String string) {
        try {
            return Long.parseLong(string);
        } catch (Exception e) {
            return 0L;
        }
    }


    public static Long parseLong(String string, long defaultValue) {
        try {
            return Long.parseLong(string);
        } catch (Exception e) {
            return defaultValue;
        }
    }


    public static Boolean parseBoolean(String string) {
        return Boolean.parseBoolean(string);
    }

    /**
     * 字节数组转成16进制字符串
     */
    public static String bytesToString(byte[] bytes) {
        StringBuilder stringBuffer = new StringBuilder(bytes.length * 2);
        String tmp;
        for (byte b : bytes) {
            tmp = (Integer.toHexString(b & 0XFF)); // 整数转成十六进制表示
            if (tmp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(tmp);
        }
        return stringBuffer.toString();
    }

    /**
     * 将hex字符串转换成字节数组
     */
    public static byte[] stringToBytes(String string) {
        if (string == null || string.length() < 2) {
            return new byte[0];
        }
        string = string.toLowerCase();
        int l = string.length() / 2;
        byte[] result = new byte[l];
        for (int i = 0; i < l; i++) {
            String tmp = string.substring(2 * i, 2 * i + 2);
            result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
        }
        return result;
    }


    /**
     * Base64编码
     */
    public static String toBase64(String string) {
        if (string == null) return null;
        return (new sun.misc.BASE64Encoder()).encode(string.getBytes());
    }

    /**
     * Base64解码
     */
    public static String toFromBase64(String string) {
        if (string == null) return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(string);
            return new String(b);
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] stringToHex(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for(int i = 0; i < digest.length; i++)
        {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte)byteValue;
        }
        return digest;
    }

    public static String hexToString(byte[] bytes) {
        //String result = "", hex;
        //String hexStr = "0123456789ABCDEF";
        //for (byte aByte : bytes) {
        //    hex = String.valueOf(hexStr.charAt((aByte & 0xF0) >> 4));//字节高4位
        //    hex += String.valueOf(hexStr.charAt(aByte & 0x0F));//字节低4位
        //    result += hex;
        //}
        //return result;

        StringBuilder hexString = new StringBuilder();
        for (byte aByte : bytes) {
            String plainText = Integer.toHexString(0xff & aByte);
            if (plainText.length() < 2) {
                plainText = "0" + plainText;
            }
            hexString.append(plainText);
        }
        return hexString.toString();
    }
}